package com.hhit.zhgd.controller

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.hhit.zhgd.component.FileProperties
import com.hhit.zhgd.domain.dto.CakeInfo
import com.hhit.zhgd.domain.entity.ZhgdEquipmentMaterialTesting
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdEquipmentMaterialTestingService
import jakarta.annotation.Resource
import org.apache.commons.lang3.StringUtils
import org.springframework.util.ObjectUtils
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.io.File
import java.util.function.Consumer

@RestController
@RequestMapping("/materialTestPlan")
class ZhgdEquipmentMaterialTestingPlanController {

    @Resource
    private val zhgdEquipmentMaterialTestingService: ZhgdEquipmentMaterialTestingService? = null

    @Resource
    private val fileProperties: FileProperties? = null


    @PostMapping("/get")
    fun page(@RequestBody info: ZhgdEquipmentMaterialTesting): BaseResp<Any?>? {
        val pageInfo = Page<ZhgdEquipmentMaterialTesting>(
            info.page!!.toLong(), info.size!!.toLong()
        )
        val wrapper = QueryWrapper<ZhgdEquipmentMaterialTesting>()
        wrapper.like(!ObjectUtils.isEmpty(info.testItem), "test_item", info.testItem)
        wrapper.like(!ObjectUtils.isEmpty(info.partUsed), "part_used", info.partUsed)
        wrapper.like(!ObjectUtils.isEmpty(info.isOk), "is_ok", info.isOk)
        wrapper.like(!ObjectUtils.isEmpty(info.type), "type", info.type)
        wrapper.orderByDesc("id")
        val page: IPage<ZhgdEquipmentMaterialTesting> = zhgdEquipmentMaterialTestingService!!.page(pageInfo, wrapper)
        return BaseResp.success(page)
    }

    @PostMapping("/add")
    fun add(@RequestBody info: ZhgdEquipmentMaterialTesting?): BaseResp<Any?>? {
        zhgdEquipmentMaterialTestingService!!.save(info)
        return BaseResp.success()
    }

    @PostMapping("/edit")
    fun edit(@RequestBody info: ZhgdEquipmentMaterialTesting?): BaseResp<Any?>? {
        zhgdEquipmentMaterialTestingService!!.updateById(info)
        return BaseResp.success()
    }

    @PostMapping("/del")
    fun del(@RequestBody info: ZhgdEquipmentMaterialTesting): BaseResp<Any?>? {
        val list = zhgdEquipmentMaterialTestingService!!.list(
            LambdaQueryWrapper<ZhgdEquipmentMaterialTesting>().`in`(
                ZhgdEquipmentMaterialTesting::id,
                info.ids
            )
        )
        list.forEach(Consumer { (_, _, _, _, _, _, _, _, _, _, _, _, _, _, okReportUrl): ZhgdEquipmentMaterialTesting ->
            if (StringUtils.isNotBlank(okReportUrl)) {
                for (fileUrl in okReportUrl!!.split(",".toRegex()).dropLastWhile { it.isEmpty() }
                    .toTypedArray()) {
                    val path: String = fileProperties?.parentPath + fileUrl
                    File(path).delete()
                }
            }
        })
        zhgdEquipmentMaterialTestingService.removeByIds(info.ids)
        return BaseResp.success()
    }

    @PostMapping("/total")
    fun total(@RequestBody info: ZhgdEquipmentMaterialTesting): BaseResp<Any?>? {
        val wra = QueryWrapper<ZhgdEquipmentMaterialTesting>()
        wra.eq("type", info.type)
        val count = zhgdEquipmentMaterialTestingService?.count(wra)?.toInt()
        wra.eq("is_check", 1)
        val countYes = zhgdEquipmentMaterialTestingService?.count(wra)?.toInt()
        val out: MutableList<CakeInfo> = ArrayList()
        out.add(CakeInfo(name = "未检测", value = count))
        out.add(CakeInfo(name = "已检测", value = countYes))
        return BaseResp.success(out)
    }
}